package com.helloandroid.activesitting.bc;

import java.util.Date;

import android.content.Context;
import android.content.SharedPreferences;

import com.helloandroid.activesitting.ApplicationObject;
import com.helloandroid.activesitting.bo.WorkTime;

public class PreferencesConnector {

	private static PreferencesConnector mInstance;

	public static PreferencesConnector GetInstance() {
		if (mInstance == null) {
			mInstance = new PreferencesConnector();
		}
		return mInstance;
	}

	SharedPreferences.Editor mEditor;

	public SharedPreferences mPreferences;
	public final static String KEY_TIME_START_MIN = "KEY_TIME_START_MIN";
	public final static String KEY_TIME_START_HOUR = "KEY_TIME_START_HOUR";
	public final static String KEY_TIME_END_MIN = "KEY_TIME_END_MIN";
	public final static String KEY_TIME_END_HOUR = "KEY_TIME_END_HOUR";
	public final static String KEY_LUNCH_START_MINUTES = "KEY_LUNCH_MINUTES";
	public final static String KEY_LUNCH_START_HOURS = "KEY_LUNCH_HOURS";
	public final static String KEY_LUNCH_END_MINUTES = "KEY_LUNCH_END_MINUTES";
	public final static String KEY_LUNCH_END_HOURS = "KEY_LUNCH_END_HOURS";
	public final static String KEY_LUNCH_ENABLED = "KEY_LUNCH_ENABLED";
	public final static String KEY_TIME_OF_REACTION = "KEY_TIME_OF_REACTION";

	private PreferencesConnector() {
		mPreferences = ApplicationObject.ApplicationContext
				.getSharedPreferences("activesitting", Context.MODE_PRIVATE);
		mEditor = mPreferences.edit();
	}

	/**
	 * Sets the worktime object to the preferences, via the supplied key
	 * 
	 * @param time
	 * @param key_min
	 * @param key_hour
	 * @return true, if everything is ok, false if not
	 */
	public boolean SetWorkTimeByKey(WorkTime time, String key_min,
			String key_hour) {
		mEditor.putInt(key_min, time.minute);
		mEditor.putInt(key_hour, time.hour);
		return mEditor.commit();

	}

//	public boolean SetKeyLunchEnabledByKey(String key, String value) {
//		mEditor.putString(key, value);
//		return mEditor.commit();
//
//	}

	/**
	 * Sets the Date object to the preferences, via the supplied key
	 * 
	 * @param key
	 * @param Date
	 *            in millisec
	 * @return true, if everything is ok, false if not
	 */
	public boolean SetTimeByKey(String key, long value) {
		mEditor.putLong(key, value);
		return mEditor.commit();
	}

	/**
	 * Gets the selected Date object from the preferences, via the supplied key
	 * 
	 * @param key
	 * @return
	 */
	public Date GetTimeByKey(String key) {
		long value = mPreferences.getLong(key, 1000);
		if (value == 1000)
			return null;
		else {
			return new Date(value);
		}
	}

	/**
	 * Sets boolean value to the preferences, via the supplied key
	 * 
	 * @param key
	 * @param value
	 * @return true, if everything is ok, false if not
	 */
	public boolean SetBooleanByKey(String key, boolean value) {
		mEditor.putBoolean(key, value);
		return mEditor.commit();
	}

	/**
	 * Gets the selected boolean value from the preferences, via the supplied
	 * key
	 * 
	 * @param key
	 * @return
	 */
	public boolean GetBooleanByKey(String key) {
		return mPreferences.getBoolean(key, false);
	}

	/**
	 * Removes the data from the preferences, via the supplied key
	 * 
	 * @param key
	 * @return true, if everything is ok, false if not
	 */
	public boolean RemoveDataByKey(String key) {
		mEditor.remove(key);
		return mEditor.commit();
	}

	/**
	 * Gets the selected worktime object from the preferences, via the supplied
	 * keys
	 * 
	 * @param key_min
	 * @param key_hour
	 * @return
	 */
	public WorkTime GetWorkTimeByKey(String key_min, String key_hour) {
		return new WorkTime(mPreferences.getInt(key_hour, 1000),
				mPreferences.getInt(key_min, 1000));
	}

}
